﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Comissoes.Models;
using Comissoes.JQGridObject;

namespace Comissoes.Controllers
{
    public class RegimesTributariosController : AppController
    {
        xcontEntities db = new xcontEntities();

        public ActionResult Index()
        {
            return View();
        }

        // Monta a lista de tipos de produtos
        public JsonResult List(String sidx, String sord, Int32 page, Int32 rows, Boolean _search, String searchField, String searchString, String searchOper)
        {
            try
            {
                var q = from c in db.REGIMES_TRIBUTARIOS
                        select new
                        {
                            act = "",
                            c.ID_RGTR,
                            c.DESC_RGTR
                        };

                JQGridBuilder jbuilder = new JQGridBuilder(q, page, sidx, sord, searchField, searchString, searchOper, "ID_RGTR", _search, rows);

                return Json(jbuilder.GetObject(), JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(null, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult Update()
        {
            RegimesTributariosModel regime;

            if (Request["id"].Equals("0"))
                regime = new RegimesTributariosModel();

            else
                regime = new RegimesTributariosModel(Int32.Parse(Request["id"]));

            if (regime.UpdateValidate(Request).Any())
                return Json(regime.UpdateValidate(Request), JsonRequestBehavior.AllowGet);

            else
                regime.Update(Request);

            return Json(true, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Delete()
        {
            RegimesTributariosModel regime = new RegimesTributariosModel(Int32.Parse(Request["id"]));

            return Json(regime.Delete(), JsonRequestBehavior.AllowGet);
        }
    }
}
